<?php
namespace jy_yuyue;
defined('BY_JYA') or exit('error');
/*
  配送订单
	
*/
class order extends \plugin{
	/*
		抢单通知
	*/
	public function order_qiang($order){
		$order_id=intval($order['id']);
		$allusers=array();
		if($order['storeid'] && !$order['merchid']){
			$areas=pdo_getall('jy_peisong_area',array('uniacid'=>$_SESSION['uniacid'],'store_id OR'=>'store_id=0 or store_id = \''.$order['storeid'].'\''));
			foreach($areas as &$item){
				$users=pdo_getall('jy_peisong_member',array('online'=>1,'uniacid'=>$_SESSION['uniacid'],'group_id'=>$item['id'],'status'=>1),'mid');
				foreach($users as $uv){
					$allusers[]=$uv;
				}
			}
		}else{
			$users=pdo_getall('jy_peisong_member',array('online'=>1,'status'=>1,'uniacid'=>$_SESSION['uniacid']),'mid');
			foreach($users as $uv){
					$allusers[]=$uv;
			}
		}
		$nums=count($allusers);
		$key=rand(0,$nums-1);
		$key=$key?:0;
		$peisong_id=$allusers[$key]['mid'];
		if(!$peisong_id){
			//return false;
		}
		//$params=array('order_id'=>$order_id,'peisong_id'=>$peisong_id);
		$this->model('notice')->notice_ordermsg($order,$allusers);
		return true;
	}
	public function order_sys($order){
		if(!$order['merchid']){
			$message_tpl=$this->plugin_setting('message_tpl');
			$admin_ids=$message_tpl['admin_ids']?:array();
			foreach($admin_ids as $av){
				$this->model('notice')->notice_orderget($order['id'],$av['mid']);
			}
		}else{
			$mid=pdo_getcolumn('jy_merch_user',array('id'=>$order['merchid'],'uniacid'=>$_SESSION['uniacid']),'mid');
			$this->model('notice')->notice_orderget($order['id'],$mid);
		}
		return '';
	}
	/*
		系统指派，peisong_id是MID
	*/
	public function order_pai($order){
		$order['order_id']=$order['id'];
		$this->order_sendget($order);
		return $mid;
	}
	/*
		抢单worker_id
	*/
	public function order_get($params){
		$order_id=intval($params['order_id']);
		$worker_id=intval($params['worker_id']);
		$mid=pdo_getcolumn('jy_yuyue_worker',array('id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']),'mid');
		$worker_id=pdo_getcolumn('jy_yuyue_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']),'worker_id');
		if($worker_id){
			exi('该订单已被别人抢单了！','error');
		}
		$this->model('notice')->notice_orderget($order_id,$mid);
		return true;
	}
	/*
		发送订单给配送员
	*/
	public function order_sendget($params){
		$order_id=intval($params['order_id']);
		$worker_id=intval($params['worker_id']);
		$mid=pdo_getcolumn('jy_yuyue_worker',array('id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']),'mid');
		$this->model('notice')->notice_orderget($order_id,$mid);
		return true;
	}
}